package com.neuresys.database.client;

import com.neuresys.database.databaseapi.Connection;
import com.neuresys.database.databaseapi.DatabaseOpenEvent;
import com.neuresys.database.databaseapi.DatabaseOpenHandler;
import com.neuresys.database.databaseapi.DatabaseSaveEvent;
import com.neuresys.database.databaseapi.DatabaseSaveHandler;
import com.neuresys.database.databaseapi.StoreDataService;
import com.neuresys.database.databaseapi.StoreDataServiceFactory;
import com.neuresys.database.databaseimpl.jssqlite.SQLite;
import com.neuresys.database.databaseimpl.jssqlite.SQLiteDatabase;
import com.neuresys.database.databaseimpl.jssqlite.SQLiteDatabaseOpenEvent;
import com.neuresys.database.implstorage.gwtmobileimpl.WebDatabaseStoreDataService;
import com.neuresys.database.implstorage.indexeddbimpl.IndexedDBStoreDataService;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.typedarrays.client.Uint8ArrayNative;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SqliteGWT implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
		/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		RootPanel.get().add(new Label("Create SQLIteDatabase"));
		final StoreDataService dataService=StoreDataServiceFactory.getInstance();
		dataService.dropAllDatabase();
		
		dataService.getDatabase("MyDatabase",new DatabaseOpenHandler(){

			@Override
			public void onSucceed(DatabaseOpenEvent event) {
				SQLite c=event.getSQLite();
				RootPanel.get().add(new Label("TABLE"+(c.exec("SELECT name FROM sqlite_master WHERE type = \"table\" ")).toString()));
				RootPanel.get().add(new Label("CREATE TABLE A (KEY INTEGER)"));
					c.exec("CREATE TABLE A (KEY INTEGER)");
					RootPanel.get().add(new Label("INSERT"));
					
	for (int i=0;i<10;i++)
	 {
		c.exec("insert into  A values(3)");
	 }
	RootPanel.get().add(new Label("FIN INSERT"));
		RootPanel.get().add(new Label((c.exec("select * from A ")).toString()));
		dataService.insertOrCreateDatabase("MyDatabase", c,new DatabaseSaveHandler(){

			@Override
			public void onSucceed(DatabaseSaveEvent event) {
				dataService.getDatabase("MyDatabase",new DatabaseOpenHandler(){

					@Override
					public void onSucceed(DatabaseOpenEvent event) {
						SQLite c=event.getSQLite();	
						RootPanel.get().add(new Label("TEST"+(c.exec("SELECT * FROM A ")).toString()));
						dataService.insertOrCreateDatabase("MyDatabase", c, null);
						
					}

					@Override
					public void onError(DatabaseOpenEvent event) {
						RootPanel.get().add(new Label("FAILED"+event));
						
					}
				
					});
			
			}
			
		});
			
		
			
		}

			@Override
			public void onError(DatabaseOpenEvent event) {
				// TODO Auto-generated method stub
				
			}});
				
	}
}
